<template>
  <div>
    <el-container class="home-container">
      <el-header>
        <span>
          <img src="../assets/img/heima.png" alt="" />
          米滋电商后台
        </span>
        <el-button
          type="danger"
          @click="logOut"
          icon="el-icon-switch-button"
          circle
        ></el-button>
      </el-header>
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <!-- 折叠菜单按钮 -->
          <div class="toggle-button" @click="changeCollapse">|||</div>
          <!-- 一级标题 -->
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#ffd04b"
            unique-opened
            :collapse-transition="false"
            :collapse="isCollapse"
            router
            :default-active="isclickMenu"
          >
            <!-- 一级菜单 -->
            <el-submenu
              :index="item.id + ''"
              v-for="item in menusList"
              :key="item.id"
            >
              <!-- 一级菜单模板 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="item.iconclass"></i>
                <!-- 文本 -->
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级子菜单 -->
              <el-menu-item
                :index="'/' + obj.path"
                v-for="obj in item.children"
                :key="obj.id"
                @click="clickMenu('/' + obj.path)"
              >
                <!-- 二级菜单模板 -->
                <template slot="title">
                  <!-- 图标 -->
                  <i :class="obj.iconclass"></i>
                  <!-- 文本 -->
                  <span>{{ obj.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-container>
          <el-main>
            <router-view></router-view>
          </el-main>
        </el-container>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { Service } from '../service/index'
export default {
  data() {
    return {
      menusList: [],
      isCollapse: false,
      isclickMenu: sessionStorage.getItem('hashPath'),
    }
  },
  async created() {
    const res = await Service.menusList()
    this.menusList = res.data.data
  },
  methods: {
    // 退出登录
    async logOut() {
      try {
        await this.$msgBox.confirm('你将退出登录,是否继续', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
        sessionStorage.removeItem('dianshangToken')
        location.reload()
      } catch (error) {
        console.log(error)
      }
    },
    // 
    changeCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 点击子菜单切换路由
    clickMenu(path) {
      this.isclickMenu = path
      window.sessionStorage.setItem('hashPath', path)
    },
  },
}
</script>

<style scoped>
.el-header {
  background-color: #373d41;
  color: #fff;
  text-align: center;
  line-height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0;
  font-size: 20px;
}
.el-header span img {
  vertical-align: middle;
}

.el-aside {
  background-color: #333744;
  color: #333;
  line-height: 200px;
}
.el-aside .el-menu {
  border-right: none;
}

.el-main {
  background-color: #eaedf1;
  color: #333;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.iconfont {
  margin-right: 10px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
.home-container {
  width: 100%;
  height: 100vh;
}
</style>